package com.repair.controller;

import com.repair.entity.RepairOrder;
import com.repair.entity.User;
import com.repair.mapper.UserMapper;
import com.repair.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
public class RepairOrderController {

    @Autowired
    private RepairOrderService repairOrderService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping
    public ResponseEntity<?> createOrder(@RequestBody RepairOrder order) {
        repairOrderService.createOrder(order);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{orderId}/accept")
    public ResponseEntity<?> acceptOrder(
            @PathVariable Long orderId,
            @RequestBody Map<String, Long> request) {
        repairOrderService.acceptOrder(orderId, request.get("repairmanId"));
        return ResponseEntity.ok().build();
    }

    @PutMapping("/{orderId}/status")
    public ResponseEntity<?> updateOrderStatus(
            @PathVariable Long orderId,
            @RequestBody Map<String, Integer> request) {
        repairOrderService.updateOrderStatus(orderId, request.get("status"));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserOrders(@PathVariable Long userId) {
        List<RepairOrder> orders = repairOrderService.getUserOrders(userId);
        for (RepairOrder order : orders) {
            // 查找维修工信息
            User user = userMapper.selectById(order.getRepairmanId());
            if (user != null) {
                order.setRepairmanName(user.getRealName());
                order.setRepairmanPhone(user.getPhone());
            } else {
                order.setRepairmanName("未知");
                order.setRepairmanPhone("未知");
            }
        }
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/repairman/{repairmanId}")
    public ResponseEntity<?> getRepairmanOrders(@PathVariable Long repairmanId) {
        List<RepairOrder> orders = repairOrderService.getRepairmanOrders(repairmanId);
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/nearby")
    public ResponseEntity<?> getNearbyOrders(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam Double radius) {
        List<RepairOrder> orders = repairOrderService.getNearbyOrders(latitude, longitude, radius);
        return ResponseEntity.ok(orders);
    }

    @PutMapping("/repairman/location")
    public ResponseEntity<?> updateRepairmanLocation(
            @RequestBody Map<String, Double> location) {
        repairOrderService.updateRepairmanLocation(
                location.get("repairmanId").longValue(),
                location.get("latitude"),
                location.get("longitude")
        );
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<?> getOrder(@PathVariable Long orderId) {
        RepairOrder order = repairOrderService.getById(orderId);
        // 查找维修工信息
        if (order != null && order.getRepairmanId() != null) {
            User user = userMapper.selectById(order.getRepairmanId());
            if (user == null) {
                order.setRepairmanName("未知");
                order.setRepairmanPhone("未知");
                return ResponseEntity.ok(order);
            }
            order.setRepairmanName(user.getRealName());
            order.setRepairmanPhone(user.getPhone());
        }
        return ResponseEntity.ok(order);
    }

    @DeleteMapping("/{orderId}")
    public ResponseEntity<?> cancelOrder(@PathVariable Long orderId) {
        repairOrderService.cancelOrder(orderId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<?> getOrdersByStatus(@PathVariable Integer status) {
        List<RepairOrder> orders = repairOrderService.getOrdersByStatus(status);
        return ResponseEntity.ok(orders);
    }
} 